import axios from 'axios'

//管理员
// 获得所有课程
export const findAllCourse = () => {
    return axios.get('/course/all')
}

// 新增课程
// export const add_course = () => {
//     return axios.post('',req)
// }


// 获得所有的学生
export const findAllStudents = () => {
    return axios.get('/student/all')
}

// 查询所有的管理员发的公告
export const findAllNoticeM = () =>
{
   return axios.get('/NoticeAdmin/all')
}


// 分页查询所有管理员发的公告
export const findAllNoticeByAdmin = (size:number,page:number) => {
    return axios.get('/NoticeAdmin/getAllPage?size='+size+'&page='+page)
}

// 分页查询所有的教师
export const findAllTeacherByPage = (size:number,page:number)=>{
    return axios.get('/teacher/getAllPage?size='+size+'&page='+page)
}

// 分页查询所有的课程
export const findAllCourseByPage = (size:number,page:number) => {
    return axios.get('/course/getAllPage?size='+size+'&page='+page)
}

// 分页查询所有的学生
export const findAllStudentByPage = (size:number,page:number) => {
    return axios.get('/student/getAllPage?size='+size+'&page='+page)
}

// 查询所有的教师
export const findAllTeachers = () => {
    return axios.get('/teacher/all')
}

// 管理员发布公告
export const createNotice = (req:{}) => {
    return axios.post('/NoticeAdmin/addNotice',req)
}

// 登录
export const login = (req:{}) => {
    return axios.get('/login/loginAdmin',req)
}


//教师
//传入
//课程的学生信息
export const findCourseStudent=()=>{
    return axios.get('/course/getstudent')
}
// export const banner
//公告
export const allNotice=()=>{
    return axios.get('/tcnotice/all')
}
//
//传出







//创建axios实例
const service = axios.create({
    baseURL: "http://localhost:8888",
    // baseURL:"https://api.apiopen.top/api",
    // baseURL:"https://www.fastmock.site/mock/88589a30789822d059bca54bf21044d8/api",
    timeout: 5000,
    headers: {
        "Content-Type": "application/json;charset=utf-8",
    },
});

//请求拦截
service.interceptors.request.use(
    (config) => {
        config.headers = config.headers || {};
        if (localStorage.getItem("token")) {
            config.headers.token = localStorage.getItem("token") || "";
        }
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);
// 响应拦截
service.interceptors.response.use(
    (res) => {
        const code: number = res.data.code;
        if (code != 200) {
            return Promise.reject(res.data);
        }
        // 兼容服务端返回的字符串数据
        if (typeof res.data === "string") {
            res.data = res.data ? JSON.parse(res.data) : res.data;
        }

        return res.data;
    },
    (error) => {
        console.log("err" + error); // for debug
        return Promise.reject(error);
    }
);

export default service;
